/*!
 Temelia - Doubly linked list performance tests.

 Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

#include <vector>
#include <algorithm>

extern "C"
{
#include <temelia/iterator.h>
#include <temelia/doubly_linked_list.h>
}

static void temelia_doubly_linked_list_search_key(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	for (int i = 0; i < n; i++)
		doubly_linked_list_search_key(list, default_collection[i], int_compare,
				NULL);

	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_search_iterator(int n)
{
	doubly_linked_list_t list;
	doubly_linked_list_iterator_t it;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	it = doubly_linked_list_get_begin(list);

	for (int i = 0; i < n; i++)
	{
		doubly_linked_list_search_iterator(list, it);
		it = doubly_linked_list_iterator_get_next(it);
	}

	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_remove_key(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	for (int i = 0; i < n; i++)
		doubly_linked_list_remove_key(list, default_collection[i], int_compare,
				NULL, 1);

	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_remove_iterator(int n)
{
	doubly_linked_list_t list;
	doubly_linked_list_iterator_t it, aux;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	it = doubly_linked_list_get_begin(list);

	for (int i = 0; i < n; i++)
	{
		doubly_linked_list_remove_iterator(list, it, 0);
		aux = it;
		it = doubly_linked_list_iterator_get_next(it);
		doubly_linked_list_iterator_delete(aux);
	}

	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_insert_after(int n)
{
	doubly_linked_list_t list;
	doubly_linked_list_iterator_t it = NULL;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
	{
		doubly_linked_list_insert_after(list, it, default_collection[i]);
		if (rand() % 2)
		{
			if (!it)
				it = doubly_linked_list_get_begin(list);
			it = doubly_linked_list_iterator_get_next(it);
		}
	}

	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_insert_before(int n)
{
	doubly_linked_list_t list;
	doubly_linked_list_iterator_t it = NULL;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
	{
		doubly_linked_list_insert_before(list, it, default_collection[i]);
		if (rand() % 2)
		{
			if (!it)
				it = doubly_linked_list_get_begin(list);
			it = doubly_linked_list_iterator_get_next(it);
		}
	}

	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_push_front(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_front(list, default_collection[i]);
	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_push_back(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);
	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_pop_front(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	for (int i = 0; i < n; i++)
		doubly_linked_list_pop_front(list);
	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_pop_back(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	for (int i = 0; i < n; i++)
		doubly_linked_list_pop_back(list);
	doubly_linked_list_delete(list);
}

static void my_iterating_function(void *key, void *context)
{
	// nothing to do...
}

static void temelia_doubly_linked_list_iterate(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	doubly_linked_list_iterate(list, my_iterating_function, NULL, 1);
	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_sort(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	doubly_linked_list_sort(list, int_compare, NULL);
	doubly_linked_list_delete(list);
}

static void temelia_doubly_linked_list_reverse(int n)
{
	doubly_linked_list_t list;

	list = doubly_linked_list_new();

	for (int i = 0; i < n; i++)
		doubly_linked_list_push_back(list, default_collection[i]);

	doubly_linked_list_reverse(list);
	doubly_linked_list_delete(list);
}

void TemeliaPerformance::pt_doubly_linked_list(void)
{
	TestUnit testUnit("doubly_linked_list");

	GENERATE_TEMELIA_TEST(testUnit, "search_key", doubly_linked_list_search_key);
	GENERATE_TEMELIA_TEST(testUnit, "search_iterator", doubly_linked_list_search_iterator);
	GENERATE_TEMELIA_TEST(testUnit, "remove_key", doubly_linked_list_remove_key);
	GENERATE_TEMELIA_TEST(testUnit, "remove_iterator", doubly_linked_list_remove_iterator);
	GENERATE_TEMELIA_TEST(testUnit, "insert_after", doubly_linked_list_insert_after);
	GENERATE_TEMELIA_TEST(testUnit, "insert_before", doubly_linked_list_insert_before);
	GENERATE_TEMELIA_TEST(testUnit, "push_front", doubly_linked_list_push_front);
	GENERATE_TEMELIA_TEST(testUnit, "push_back", doubly_linked_list_push_back);
	GENERATE_TEMELIA_TEST(testUnit, "pop_front", doubly_linked_list_pop_front);
	GENERATE_TEMELIA_TEST(testUnit, "pop_back", doubly_linked_list_pop_back);
	GENERATE_TEMELIA_TEST(testUnit, "iterate", doubly_linked_list_iterate);
	GENERATE_TEMELIA_TEST(testUnit, "reverse", doubly_linked_list_reverse);

#ifndef CLUSTER_RUNNING
	ntests = 6;
#endif

	GENERATE_TEMELIA_TEST(testUnit, "sort", doubly_linked_list_sort);

	testUnit.write(fout);
}
